import { getStream } from '../Core/connect'
import { Polygon } from './Polygon'
import { Material } from '../Material/Material'

export class Ground extends Polygon {
    readonly type: string
    constructor(name: string, Height: number = 1000) {
        super(name, Height)
        this.type = 'Ground'
    }
    // 更新地面铺装材质
    replaceGroundMaterial(Material: Material) {
        const that = this;
        return new Promise(function (resolve) {
            const stream = getStream();
            const str = { "function": "replaceGroundMaterial", ids: `${that.uuid}`, Material: Material };

            stream.emitUIInteraction(str);

            const replaceGroundMaterial = (data: any) => {
                const returnData = JSON.parse(data);
                if (returnData.uuid === `${that.uuid}`) {
                    stream.removeResponseEventListener(`replaceGroundMaterial${that.uuid}`, replaceGroundMaterial);
                    resolve(returnData);
                }
            };

            stream.addResponseEventListener(`replaceGroundMaterial${that.uuid}`, replaceGroundMaterial);
        });
    }
    // 设置铺装颜色
    setGroundMaterial(Material: Material) {
        const that = this;
        return new Promise(function (resolve) {
            const stream = getStream();
            const str = { "function": "setGroundMaterial", ids: `${that.uuid}`, Material: Material };

            stream.emitUIInteraction(str);

            const setGroundMaterial = (data: any) => {
                const returnData = JSON.parse(data);
                if (returnData.uuid === `${that.uuid}`) {
                    stream.removeResponseEventListener(`setGroundMaterial${that.uuid}`, setGroundMaterial);
                    resolve(returnData);
                }
            };

            stream.addResponseEventListener(`setGroundMaterial${that.uuid}`, setGroundMaterial);
        });
    }
}

